package com.pipayshop.controller;

import cn.hutool.core.lang.hash.Hash64;
import com.pipayshop.domain.dto.GetOrderByNameDto;
import com.pipayshop.domain.dto.GetOrderDataDto;
import com.pipayshop.domain.vo.ShopOrderDetailVO;
import com.pipayshop.domain.vo.ShopOrderInfoVO;
import com.pipayshop.entity.vo.GetOrderDataVO;
import com.pipayshop.entity.vo.PageDataVO;
import com.pipayshop.entity.vo.ResponseVO;
import com.pipayshop.exception.BusinessException;
import com.pipayshop.service.OnlineStoreShopOrderInfoService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author xiaoqiu
 * @date 2024/9/20 10:57
 * @description 网店订单接口
 */
@RestController
@RequestMapping("/pipayshopapi/order")
@Slf4j
public class OnlineStoreShopOrderController {


    @Autowired
    private OnlineStoreShopOrderInfoService shopOrderInfoService;

    /**
     * 查看未支付/已支付/已完成订单列表接口(通过一个标识id来获取对应的状态的列表展示)
     *  标识id -1：所有订单   0：未支付订单    1：已支付订单   2：已完成（已经收货）订单
     */
    @PostMapping("getOrderList")
    @ApiOperation("（买家/卖家）用户的全部网店订单列表分页展示标识id -1：所有订单   0：未支付订单    1：已支付订单   2：已完成（已经收货）订单")
    public ResponseVO<List<ShopOrderInfoVO>> getOrderList(@RequestBody GetOrderDataDto getOrderDataDto) {
        try {
            List<ShopOrderInfoVO> list = shopOrderInfoService.getOrderList(getOrderDataDto);
            if (list == null){
                return ResponseVO.getSuccessResponseVo(null);
            }
            return ResponseVO.getSuccessResponseVo(list);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("order_list_display_failed");
        }

    }

    @PostMapping("getOrderListByItemName")
    @ApiOperation("（买家/卖家）用户的全部网店订单列表展示标识id -1：所有订单   0：未支付订单    1：已支付订单   2：已完成（已经收货）订单")
    public ResponseVO<List<ShopOrderInfoVO>> getOrderListByItemName(@RequestBody GetOrderByNameDto getOrderByNameDto) {
        try {
            List<ShopOrderInfoVO> list = shopOrderInfoService.getOrderListByItemName(getOrderByNameDto);
            if (list == null){
                return ResponseVO.getSuccessResponseVo(null);
            }
            return ResponseVO.getSuccessResponseVo(list);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("order_list_display_failed");
        }
    }

    /**
     删除订单接口
     */
    @PostMapping("delOrder/{orderId}")
    @ApiOperation("删除订单接口")
    public ResponseVO<String> delOrderByOrderId(@PathVariable String orderId) {
        try {
            int delete = shopOrderInfoService.delOrderByOrderId(orderId);
            if (delete < 1){
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("delete_order_success");
        }catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("delete_order_failed");
        }
    }

    /**
     未支付订单改价接口
     */
    @PostMapping("changePrice")
    @ApiOperation("未支付订单改价接口")
    public ResponseVO<String> changePrice(String token) {
        try {
            int update = shopOrderInfoService.changePrice(token);
            if (update < 1){
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("update_unpaid_order_price_success");
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        }  catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("update_unpaid_order_price_failed");
        }

    }


    /**
     支付下单接口
     */
    @PostMapping("payOrder")
    @ApiOperation("支付下单接口")
    public ResponseVO<String> payOrder(String token) {
        try {
            boolean flag = shopOrderInfoService.payOrder(token);
            if (!flag) {
                return ResponseVO.getFalseResponseVo("order_already_paid");
            }
            return ResponseVO.getSuccessResponseVo("payment_order_success");
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        }  catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("payment_order_failed");
        }
    }

    /**
     pi支付上传凭证
     * @return
     */
    @PostMapping("payOrderCertificate")
    @ApiOperation("pi支付上传凭证")
    public ResponseVO payOrderCertificate(MultipartFile file, String token) {
        try {
            boolean flag = shopOrderInfoService.payOrderCertificate(file,token);
            if (!flag) {
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("pi_payment_upload_success");
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        }  catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("pi_payment_upload_failed");
        }
    }

    @PostMapping("/updateOrder/{orderId}/{status}")
    public ResponseVO<String> updateOrder(@PathVariable("orderId") String orderId,@PathVariable("status") Integer status){
        try {
            Integer update = shopOrderInfoService.updateOrder(orderId, status);
            if (update < 1){
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("update_order_success");
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        }  catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("update_order_failed");
        }

    }

    /**
     订单的详情接口
     */
    @GetMapping("getOrderDetail/{orderId}")
    @ApiOperation("订单详情接口")
    @Transactional
    public ResponseVO<ShopOrderDetailVO> getOrderDetail(@PathVariable String orderId) {
        try {
            ShopOrderDetailVO orderDetail = shopOrderInfoService.getOrderDetail(orderId);
            if (orderDetail == null){
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo(orderDetail);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("get_order_details_failed");
        }
    }

    /**
     * 生成未支付订单号
     * @param token
     * @return
     */
    @PostMapping("/generateUnpaidOrder")
    public ResponseVO<List<String>> generateUnpaidOrder(String token){
        try {
            return ResponseVO.getSuccessResponseVo(shopOrderInfoService.generateUnpaidOrder(token));
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("generate_unpaid_order_no_failed");
        }
    }

    /**
     订单超时未支付的失效操作
     */
    @PostMapping("failOrder/{orderId}")
    @ApiOperation("订单超时未支付的失效操作接口")
    public ResponseVO<String> failOrder(@PathVariable String orderId) {
        try {
            shopOrderInfoService.failOrder(orderId);
            return ResponseVO.getSuccessResponseVo("check_order_timeout_success");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("check_order_timeout_failed");
        }
    }

    /**
     订单改为已完成状态接口
     */
    @PostMapping("completedOrder/{orderId}")
    @ApiOperation("订单已完成接口")
    public ResponseVO<String> completedOrder(@PathVariable String orderId) {
        try {
            int update = shopOrderInfoService.completedOrder(orderId);
            if (update < 1){
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("order_confirm_success");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("order_confirm_failed");
        }
    }
//    /**
//     * 查看未支付/已支付/已完成订单列表接口(通过一个标识id来获取对应的状态的列表展示)
//     *  标识id -1：所有订单   0：未支付订单    1：已支付订单   2：已完成（已经收货）订单
//     */
//    @GetMapping("getOrderListByShopId")
//    @ApiOperation("(卖家)网店店的全部订单列表分页展示标识id -1：所有订单   0：未支付订单    1：已支付订单   2：已完成（已经收货）订单")
//    public ResponseVO getOrderListByShopId(GetOrderDataDto getOrderDataDto) {
//
//            PageDataVO list = shopOrderInfoService.getOrderListByShopId(getOrderDataDto);
//            if (list == null){
//                return ResponseVO.getFalseResponseMsg("获取订单列表失败");
//            }
//            return ResponseVO.getSuccessResponseVo(list);
//
//    }
}
